package cn.madog.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    private static ArrayList<Question> questions = new ArrayList<Question>();
    private static HashMap<String, List<Question>> publicQuestionMains = new HashMap<String, List<Question>>();
    private static Question question = null;

    private static HashMap<String, String> answerList = null;


    public static void handleNameAndType() {


    }

    public static void main(String[] args) throws IOException {


        String str = "（5～16题共用题干）";


        Pattern pattern = Pattern.compile("\\d{1,2}\\d{1,3}");

        Matcher matcher = pattern.matcher(str);
        matcher.find();

        System.out.println(Integer.parseInt(matcher.group()));

        matcher.find();

        System.out.println(Integer.parseInt(matcher.group()));


//        String str = "130. 在该社区高血压防治过程中，鼓励人群改变不良行为和生活方式属于";

//        new XSSFWorkbook()


//        System.out.println(str.matches(".?\\d{1,3}～\\d{1,3}题共用题.*"));
//
//
//        Pattern pattern = Pattern.compile("\\d{1,4}\\.[A-Ea-e]");
////        Pattern pattern = Pattern.compile("(?!\\d{1,4}|\\.).*");
//
//        Matcher matcher = pattern.matcher(str);
//
//        matcher.find();
//        System.out.println(matcher.group());
//
//        matcher.find();
//        System.out.println(matcher.group());


//        String str = "大121不腻大范甘迪34 二34vv ";
//        String regEx="\\d{1,3}";
//        Pattern p= Pattern.compile(regEx);
//        Matcher m = p.matcher(str);
//        List re=new ArrayList();
//
//        while(m.find()){
//            re.add(m.group());
//        }
//        System.out.println(re.get(re.size()-1));
//        if (matcher.find())


//        String[] split = str.split("（\\d{1,3}～\\d{1,3}题共用题干）");
//        System.out.println(str.matches("（\\d{1,3}～\\d{1,3}题共用题干）"));


//        handleNameAndType();
//
//
//        FileInputStream inputStream = new FileInputStream(new File("test.txt"));
//
//        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//
//        String line = "";
//
//        while ((line = reader.readLine()) != null) {
//            System.out.println(line);
//
//            if ("end".equals(line)) {
//                continue;
//            }
//
//            if (handleQuestion(line)) {
//                continue;
//            }
//
//        }
//
//        questions.forEach(System.out::println);
//
//        XSSFWorkbook workbook = ExcelExportUtils.export(questions);
//        workbook.write(new FileOutputStream("a1.xlsx"));
//
//        if (publicQuestionMains != null) {
//
//            System.out.println("有a3a4题");
//
//            XSSFWorkbook workbookA3 = ExcelExportUtils.exportA3(publicQuestionMains);
//            workbookA3.write(new FileOutputStream("a3a4.xlsx"));
//        }
//
//        System.out.println("所有题读取完成");
//
//        reader.close();
    }


    private static boolean handleRightAnswer(String line) {
        if (line.contains("【答案】")) {
//            System.out.println();
//            System.out.println("读取到答案");
//            System.out.println();
            String s = line.replace("【答案】", "");
            question.setQuestionAnswer(s);
            return true;
        }
        return false;
    }

    private static boolean handleQuestion(String[] split) {

        if (split.length >= 2) {

            try {

                int questionId = Integer.parseInt(split[0]);

                String questionMain = split[1];

                StringBuilder value = new StringBuilder();
                for (int i = 1; i < split.length; i++) {
                    value.append(".").append(split[i]);
                }

//                if (publicQuestionMain != null) {
//                    if (publicQuestionMain.contains(questionId)) {
//                        questionMain = publicQuestionMainStr + value.toString();
//                    }
//                }else {
                questionMain = value.toString();
//                }

                System.out.println("------------------ " + questionMain);
                question.setQuestionId(questionId);
                question.setQuestionMain(questionMain);
//                System.out.println();
//                System.out.println("################ 读取到题干");
//                System.out.println();
                return true;
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }

        return false;
    }


    // （81～82题共用题干）
    private static boolean handlePublicQuestionMain(String line) {

        if (line.contains("共用题干")) {

            publicQuestionMain = null;
            publicQuestionMain = new ArrayList<>();

//            System.out.println("读取到共用题干");

            String substring = line.substring(0, line.indexOf("题"));

            String replace = substring.replace("（", "");
            String[] split = replace.split("～");

            for (int i = 0; i < split.length; i++) {
                try {
                    int parseInt = Integer.parseInt(split[i].trim());
                    publicQuestionMain.add(parseInt);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    private static boolean handleAnalysis(String line) {

        if (line.contains("【解析】")) {
            isEnd = true;
//            System.out.println();
//            System.out.println("读取到解析");
//            System.out.println();
            String s = line.replace("【解析】", "");
            question.setQuestionAnalysis(s);

            handleCurrentQuestionEnd(line);
            return true;
        }

        return false;
    }

    private static boolean handleAnswerList(String[] split) {
        if (split.length >= 2) {
//
//            System.out.println();
//            System.out.println("--------------------- 是选择题");
//            System.out.println();

            answerList.put(split[0], split[1].trim());
            return true;
        }
        return false;
    }

    private static void handleCurrentQuestionEnd(String line) {

        // 读取一题完成
//        if ("".equals(line) || isEnd) {

        question.setAnswerList(answerList);

        // A3A4题型处理
        if (publicQuestionMain != null && publicQuestionMain.contains(question.getQuestionId())) {

            if (!publicQuestionMains.containsKey(publicQuestionMainStr) || publicQuestionMains.get(publicQuestionMainStr) == null) {
                publicQuestionMains.put(publicQuestionMainStr, new ArrayList<>());
            }

            List<Question> questions = publicQuestionMains.get(publicQuestionMainStr);
            questions.add(question);
        } else {
            questions.add(question);
        }

//            System.out.println();
//            System.out.println("读取一题完成");
        question = null;
        answerList = null;
        isEnd = false;
//        }
//
//        return false;
    }

    private static ArrayList<Integer> publicQuestionMain = null;
    private static String publicQuestionMainStr = null;
    private static boolean nextQuestionMain = false;

    private static boolean isEnd = false;

    private static boolean handleQuestion(String line) {
        if (question == null) {
            question = new Question();
        }

        if (answerList == null) {
            answerList = new HashMap<String, String>();
        }

        if (publicQuestionMains == null) {
            publicQuestionMains = new HashMap<String, List<Question>>();
        }

        if (nextQuestionMain) {
            publicQuestionMainStr = line;
            nextQuestionMain = false;
            return true;
        }

        if (handleAnalysis(line)) return true;

//        if (handleCurrentQuestionEnd(line)) return true;

        if (handlePublicQuestionMain(line)) {
            nextQuestionMain = true;
            return true;
        }

        String[] split = line.split("\\.");

        if (handleRightAnswer(line)) return true;

        if (handleQuestion(split)) return true;

        if (handleAnswerList(split)) return true;

        return true;
    }
}
